package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.support.annotation.LayoutRes;
import android.view.View;

import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;

/**
 * Created by zxh on 16/12/6.
 */

public class AnswerItem extends BindingListItem {
    private static int[] rankImages;
    private int rankRes;
    private String avatar;
    private int uid;
    private int answerId;
    private int likeCount;
    private double rewardAmount;
    private String job;
    private String nickName;
    private Date answerTime;
    private String content;
    private QuestionItem question;
    private RewardDetail parent;
    private boolean showMoney;

    public RewardDetail getParent() {
        return parent;
    }

    public void setParent(RewardDetail parent) {
        this.parent = parent;
    }

    @Bindable
    public int getRankRes() {
        return rankRes;
    }

    public void setRankRes(int rankRes) {
        if (rankRes >= 5 || rankRes < 0) {
            this.rankRes = 0;
        } else {
            this.rankRes = rankImages[rankRes];
        }
        notifyPropertyChanged(BR.rankRes);
    }

    public int getAnswerId() {
        return answerId;
    }

    public void setAnswerId(int answerId) {
        this.answerId = answerId;
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    @Bindable
    public int getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(int likeCount) {
        this.likeCount = likeCount;
        notifyPropertyChanged(BR.likeCount);
    }

    @Bindable
    public boolean isShowMoney() {
        return showMoney;
    }

    public void setShowMoney(boolean showMoney) {
        this.showMoney = showMoney;
        notifyPropertyChanged(BR.showMoney);
    }

    @Bindable
    public double getRewardAmount() {
        return rewardAmount;
    }

    public void setRewardAmount(double rewardAmount) {
        this.rewardAmount = rewardAmount;
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Bindable
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Bindable
    public Date getAnswerTime() {
        return answerTime;
    }

    public void setAnswerTime(Date answerTime) {
        this.answerTime = answerTime;
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public QuestionItem getQuestion() {
        return question;
    }

    static {
        rankImages = new int[5];
        rankImages[0] = R.mipmap.rank1;
        rankImages[1] = R.mipmap.rank2;
        rankImages[2] = R.mipmap.rank3;
        rankImages[3] = R.mipmap.rank4;
        rankImages[4] = R.mipmap.rank5;
    }

    public AnswerItem(Context context, @LayoutRes int layoutId, QuestionItem question) {
        super(context, layoutId);
        this.question = question;
    }

    public void onClickAvatar(View view) {
        HomeActivity.showPersonalCentral(getContext(), getUid());
    }

    /**
     * 回答点赞
     *
     * @param view
     */
    public void onClickLike(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = WebApi.answerLike(getContext(), answerId);
                if (count > 0) {
                    setLikeCount(count);
                    showToast("已赞");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (parent != null) {
                                parent.load(1);
                            }
                        }
                    });
                }
            }
        }).start();
    }

    /**
     * 采纳此回答
     *
     * @param view
     */
    public void onClickAccept(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.answerAccept(getContext(), getAnswerId())) {
                    showToast("此回答已采纳");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (parent != null) {
                                parent.load(1);
                            }
                        }
                    });
                }
            }
        }).start();
    }

    public void onClickItem(View view) {
        AnswerDetail detail = new AnswerDetail(getContext(), this);
        CommonFragment fragment = new CommonFragment();
        fragment.setDataModel(detail);
        fragment.setLayoutId(R.layout.f_answer_detail);
        fragment.setVariableId(BR.detail);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    /**
     * 否决此回答
     *
     * @param view
     */
    public void onClickReject(View view) {

    }

}
